import { Toast } from "vant"


export const openLoading = () => {
  return Toast.loading({
    duration: 0,
    message: '加载中...',
    forbidClick: true,
  })
}

export const closeLoading = () => {
  Toast.clear()
}

export const toast = (msg, position = 'bottom') => {
  Toast({
    position: position,
    message: msg
  })
}

function findValuesInsideParentheses(str) {
  let values = [];
  let currentValue = '';
  let insideParentheses = false;

  for (let i = 0; i < str.length; i++) {
    if (str[i] === ')') {
      insideParentheses = true;
      currentValue = '';
    } else if (str[i] === '(') {
      insideParentheses = false;
      values.push(currentValue);
    } else if (insideParentheses) {
      currentValue += str[i];
    }
  }
  values.splice(0, 1)
  return values;
}

export const parseQRCode = (code, gcdm) => {
  // let CodeNo = code.match(/(?<=[)]).*?(?=[(])/g);

  // var regex = new RegExp("(?<=[)]).*?(?=[(])", "g");
  // let CodeNo = regex.exec(code);
  const CodeNo = findValuesInsideParentheses(code);
  console.log(CodeNo);
  if (CodeNo.length == 6) {
    if (gcdm == "CN01") {
      code = code + "()";
      let CodeNotwo = code.match(/(?<=[)]).*?(?=[(])/g);
      return {
        materialCode: CodeNotwo[0],//物料
        quantity: Number(CodeNotwo[1]),
        prodDate: CodeNotwo[4],
        palletNo: CodeNotwo[6],
        uom: CodeNotwo[2],//单位
        batchCod: CodeNotwo[3],//玛氏批次
        BBD: CodeNotwo[5],//到期日
        commerialVendor: "10963",//供应商
        gsVendor: "10963",//生产商
        poNo: "",//po号
        inspectionNumber: "",//报检号
        vendorBatch: "",//供应商批次
        materialCfg: "",
      }
    }
  } else {
    let materialReg = CodeNo[0]//物料
    let quantityReg = CodeNo[1]//数量
    let ProdDateReg = CodeNo[4]//生产日期
    // alert('1')
    let palletNoReg = CodeNo[8]//拍号
    console.log('3');
    let proid = CodeNo[6]
    let supid = CodeNo[7]
    console.log('1');
    if (materialReg && quantityReg && palletNoReg && proid && supid) {
      if (code.match(/[(]0[)](\d+)/)) {
        let SerialNoReg = code.match(/[(]0[)]([0-9]+)/)
        return {
          materialCode: materialReg,
          quantity: Number(quantityReg),
          prodDate: ProdDateReg,
          palletNo: palletNoReg,
          uom: CodeNo[2],//单位
          batchCod: CodeNo[3],//玛氏批次
          BBD: CodeNo[5],//到期日
          commerialVendor: CodeNo[6],//供应商
          gsVendor: CodeNo[7],//生产商
          poNo: CodeNo[9],//po号
          inspectionNumber: CodeNo[10] == "/" ? "" : CodeNo[10],//报检号
          vendorBatch: CodeNo[11],//供应商批次
          materialCfg: CodeNo[12],
          SerialNo: SerialNoReg[1]
        }
      } else {
        // let materialCfgReg = code.match(/[(]98[)](\d+)/)//物料规格
        let indexMat = code.indexOf('(98)');
        console.log(indexMat);
        let materialCfgReg = code.substring(indexMat + 4, code.length)
        console.log(materialCfgReg);
        let materialCfgRegNew = ''
        if (!materialCfgReg) {
          materialCfgRegNew = materialCfgReg
        } else {
          materialCfgRegNew = materialCfgReg[1]
        }
        return {
          materialCode: materialReg,
          quantity: Number(quantityReg),
          prodDate: ProdDateReg,
          palletNo: palletNoReg,
          uom: CodeNo[2],//单位
          batchCod: CodeNo[3],//玛氏批次
          BBD: CodeNo[5],//到期日
          commerialVendor: CodeNo[6],//供应商
          gsVendor: CodeNo[7],//生产商
          poNo: CodeNo[9],//po号
          inspectionNumber: CodeNo[10] == "/" ? "" : CodeNo[10],//报检号
          vendorBatch: CodeNo[11],//供应商批次
          materialCfg: materialCfgRegNew == null ? "/" : materialCfgRegNew
        }
      }

    }
    return null
  }

}

export const parseKuKwei = (code) => {
  let CodeNo = code.match(/(?<=[)]).*?(?=[(])/g);
  return {
    kuWeiId: CodeNo[0],
    kuWei: CodeNo[1]
  }
}

// 判断是否过期
export const ifOverdure = (dateone, datetwo) => {
  let datenow = new Date(dateone);
  let expdate = new Date(datetwo);
  if (datenow.getTime() - expdate.getTime() <= 0) {
    return false;
  } else {
    return true;
  }

}

